﻿@inject IJSRuntime JS
@inject IDataCacheService _dataCacheService
@inject IFileUploadService _fileUploadService


<MMarkdown Options="_options" Value="@Context" ValueChanged="ValueChanged" @ref="mMarkdown" Class="@Class">
</MMarkdown>

@code {
    [Parameter]
    public string Context { get; set; }
    [Parameter]
    public EventCallback<string> ContextChanged { get; set; }
    [Parameter]
    public int MinHeight { get; set; }
    [Parameter]
    public int Height { get; set; }
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public MarkdownEditorMode Mode { get; set; }

    private Dictionary<string, object> _options = new Dictionary<string, object>();

    MMarkdown mMarkdown;
    private DotNetObjectReference<MasaMarkdown> objRef;


    protected override void OnInitialized()
    {
        if (MinHeight != 0)
        {
            _options.Add("minHeight", 800);

        }
        if (Height != 0)
        {
            _options.Add("height", 800);
        }
        _options.Add("mode", Mode.ToString());
        if (_dataCacheService.ThemeSetting.IsDark)
        {
            _options.Add("theme", "dark");
        }
        _options.Add("width", "100%");
        _options.Add("upload", new
        {
            url = ToolHelper.ImageApiPath + "api/files/Upload",
            max = 10 * 1024 * 1024,
            accept = "image/*",
            multiple = true,
            linkToImgUrl = ToolHelper.ImageApiPath + "api/files/linkToImgUrl"
        });
    }

    protected override async Task OnAfterRenderAsync(bool firstRender)
    {
        if (firstRender)
        {
           await InitEditor();
        }
    }

    public async Task InitEditor()
    {
        await Task.Delay(2000);
        objRef = DotNetObjectReference.Create(this);
        await JS.InvokeVoidAsync("initVditorContext", objRef, mMarkdown.Ref);
    }

    public async Task ValueChanged(string text)
    {
        Context = text;
        await ContextChanged.InvokeAsync(text);
    }

    [JSInvokable]
    public async Task HandleUploadSuccessAsync(string res)
    {
        try
        {
            var model = JsonSerializer.Deserialize<List<UploadResult>>(res, ToolHelper.options);

            foreach (var item in model)
            {
                if (mMarkdown != null)
                {
                    await mMarkdown.InsertValueAsync($"\n![]({item.Url})\n");
                }
            }
        }
        catch
        {

        }


    }

    [JSInvokable]
    public async Task HandleLinkToImgFormatAsync(string res)
    {
        try
        {
            var model = JsonSerializer.Deserialize<UploadResult>(res, ToolHelper.options);
            if (mMarkdown != null && model.Url != model.OriginalUrl && string.IsNullOrWhiteSpace(model.Url) == false)
            {
                Context = Context.Replace(model.OriginalUrl, model.Url);
                await ContextChanged.InvokeAsync(Context);
            }
        }
        catch
        {

        }


    }
    /// <summary>
    /// 在光标位置插入文本
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    public async Task InsertContext(string context)
    {
        if (mMarkdown != null)
        {
            await mMarkdown.InsertValueAsync(context);

        }
    }
}
